package yz;

/**
 * [141. 环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)
 *
 * @author Yz
 * @since 2022-11-07 20:07
 */
public class LeetCode141_LinkedListCycle {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }
    
    static class ListNode {
        int val;
        ListNode next;
        
        public ListNode() {
        }
        
        public ListNode(int val) {
            this.val = val;
        }
        
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
